package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public void reg(VehicleSaveParam vehicleSaveParam) {
        log.debug("车辆注册业务:vehicleSaveParam={}",vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if(vehicle.getId()==null){
            vehicle.setCreateTime(new Date());
            vehicle.setUpdateTime(new Date());
            vehicle.setStatus("1");
            vehicleMapper.insert(vehicle);
        }else {
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public List<VehicleVO> select(VehicleQuery vehicleQuery) {
        log.debug("车辆查询业务:vehicleQuery={}",vehicleQuery);
        List<VehicleVO> VehicleVOS = vehicleMapper.select(vehicleQuery);
        return VehicleVOS;
    }

    @Override
    public void delete(Long id) {
        log.debug("车辆删除业务:id={}",id);
        vehicleMapper.deleteVehicleById(id);
    }

    @Override
    public void unbind(Long id) {
        log.debug("车辆解绑业务:id={}",id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bind(Long id, Long geoId) {
        log.debug("车辆绑定业务:id={},geoId={}",id,geoId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geoId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
